<script lang="ts">
import { defineComponent } from "vue";

export default defineComponent({
  props: {
    modelValue: { type: String, required: true },
  },
  emits: {
    "update:modelValue": (value: string) => typeof value === "string",
    search: (value: string) => typeof value === "string",
  },
  setup(props, ctx) {
    const updateModelValue = (e: MiniApp.Event<{ value: string }>) => {
      ctx.emit("update:modelValue", e.detail.value);
    };

    const searchClick = (type: "search" | "clear") => {
      ctx.emit("search", type);
    };

    return { updateModelValue, searchClick };
  },
});
</script>

<template>
  <view class="search-bar">
    <view class="search-bar-wrapper">
      <icon type="search" size="32rpx" color="#999999" />
      <input
        class="search-bar-input"
        :value="modelValue"
        confirm-type="search"
        focus
        placeholder="输入搜索关键词"
        @input="updateModelValue"
        @confirm="searchClick('search')"
      />
      <icon
        v-show="modelValue.length !== 0"
        type="clear"
        size="32rpx"
        color="#999999"
        @click="searchClick('clear')"
      />
      <view class="search-bar-btn" @click="searchClick('search')">搜索</view>
    </view>
  </view>
</template>

<style lang="scss">
.search-bar {
  padding: 16rpx 30rpx;

  .search-bar-wrapper {
    display: flex;
    align-items: center;
    height: 64rpx;
    padding-left: 30rpx;
    padding-right: 20rpx;
    border-radius: 36rpx;
    border: 2rpx solid rgba(255, 121, 0, 0.1);
  }

  .search-bar-input {
    flex-grow: 1;
    min-width: 0;
    margin-left: 10rpx;
    margin-right: 20rpx;
    font-size: 24rpx;
    color: #333333;
    line-height: 34rpx;
  }

  .search-bar-btn {
    flex-shrink: 0;
    padding: 0 24rpx;
    margin-left: 20rpx;
    font-size: 24rpx;
    color: #ffffff;
    line-height: 46rpx;
    font-weight: bold;
    border-radius: 24rpx;
    background: linear-gradient(90deg, #f09027 0%, #ff7900 100%);
  }
}
</style>